(function() {
    // 获取提示表单的接口
    const CenterByDSA = 'moonKnight/api/restrict/biz/operate/enterSellerCenterByDSA';
    // 获取消息的接口
    const getMsgUrl = 'bg/quick/api/merchant/msgBox/unreadMsgDetail';
    // 获取答题的接口
    const getAnswersUrl = 'marvel-app/cn/api/kiana/hawkeye/course/exam/queryCourseExam';
    // 销量数据接口
    const orDers = 'marvel/cn/api/sales/management/web/queryStatisticDataSupplier';
    // 结算数据接口
    const payment = 'api/merchant/payment/account/amount/info';
    // 关注人数接口
    const follow = 'marvel-mms/cn/api/kiana/venom/sales/management/queryMallFollowerNum';
    // 消费者咨询接口
    const consumer = 'gmp/bg/thor/api/knowledgeMms/queryMmsKnowledgeGoodsStatistics';

    // 获取注入时传递的数据
    const closeForm = JSON.parse(document.currentScript.getAttribute('data-closeForm'));
    const Cancelcountdown = JSON.parse(document.currentScript.getAttribute('data-Cancelcountdown'));
    const Answerassistant = JSON.parse(document.currentScript.getAttribute('data-Answerassistant'));
    const Show = JSON.parse(document.currentScript.getAttribute('data-Show'));

    // 全局定义 readResponseBody 函数
    window.readResponseBody = async function(response) {
        const reader = response.body.getReader();
        const decoder = new TextDecoder('utf-8');
        let responseBody = '';

        while (true) {
            const { done, value } = await reader.read();
            if (done) {
                break;
            }
            responseBody += decoder.decode(value, { stream: true });
        }
        return responseBody;
    };

    // 封装一个函数，用于创建修改后的 Response 对象
    function createModifiedResponse(responseData, originalResponse) {
        const modifiedResponse = new Response(JSON.stringify(responseData), {
            status: originalResponse.status,
            statusText: originalResponse.statusText,
            headers: originalResponse.headers
        });
        return modifiedResponse;
    }

    // 拦截 XMLHttpRequest
    // const originalXHROpen = XMLHttpRequest.prototype.open;
    // XMLHttpRequest.prototype.open = function(method, url, ...rest) {
    //     this._url = url; // 保存 URL 以供后续使用
    //     console.log('XMLHttpRequest URL:', url); // 调试信息
    //     originalXHROpen.apply(this, arguments);
    // };

    // 拦截 Fetch API
    const originalFetch = window.fetch;
    window.fetch = async function(input, init) {
        const url = typeof input === 'string' ? input : input.url;
        const response = await originalFetch.apply(this, arguments);

        if (url.includes(CenterByDSA)) {
            console.log('Intercepted Fetch API to:', url); // 调试信息

            if (closeForm) {
                try {
                    const responseBody = await window.readResponseBody(response);
                    const responseData = JSON.parse(responseBody);
                    // 在这里对返回的 JSON 数据进行处理
                    console.log('返回的JSON数据:', responseData);
                    responseData.result.restrict= false;
                    // 返回修改后的响应对象
                    const modifiedResponse = createModifiedResponse(responseData, response);
                    return modifiedResponse;
                } catch (error) {
                    console.error('处理响应时出错:', error);
                    return response;
                }
            }
            return response;
        }

        if (url.includes(getMsgUrl)) {
            console.log('Intercepted Fetch API to:', url); // 调试信息
            if (Cancelcountdown) {
                try {
                    const responseBody = await window.readResponseBody(response);
                    const responseData = JSON.parse(responseBody);
                    console.log('返回的JSON数据:', responseData);
                    // 确保 responseData 存在并且结构正确
                    if (responseData.result && responseData.result.unreadPopMsg) {
                        // 遍历 unreadPopMsg 数组中的每个元素，修改 forceReadSecond 属性为 0
                        responseData.result.unreadPopMsg.forEach(msg => {
                            msg.forceReadSecond = 0; // 修改 forceReadSecond 属性值为 0
                        });
                    }
        
                    // 返回修改后的响应对象
                    const modifiedResponse = createModifiedResponse(responseData, response);
                    return modifiedResponse;
                } catch (error) {
                    console.error('处理响应时出错:', error);
                    return response;
                }
            }
            return response;
        }

        if (url.includes(getAnswersUrl)) {
            console.log('Intercepted Fetch API to:', url); // 调试信息
            if (Answerassistant) {
                try {
                    const responseBody = await window.readResponseBody(response);
                    const responseData = JSON.parse(responseBody);
                    console.log('返回的JSON数据:', responseData);
                    if (responseData.result && responseData.result.questionList) {
                        responseData.result.questionList.forEach(msg => {
                            msg.optionList.forEach(item => {
                                if (item.isTrue) {
                                  item.optionContent = "正确答案";
                                }
                            });
                        });
                    }
                    // 返回修改后的响应对象
                    const modifiedResponse = createModifiedResponse(responseData, response);
                    return modifiedResponse;
                } catch (error) {
                    console.error('处理响应时出错:', error);
                    return response;
                }
            }
            return response;
        }

        if (url.includes(orDers)) {
            console.log('Intercepted Fetch API to:', url); // 调试信息
            if (Show) {
                try {
                    const responseBody = await window.readResponseBody(response);
                    const responseData = JSON.parse(responseBody);
                    console.log('返回的JSON数据:', responseData);
                    // 即将售罄
                    responseData.result.aboutToSellOutNumber= 97;
                    // 调价待确认
                    responseData.result.adjustPrice= 187;
                    // 建议备货
                    responseData.result.advicePrepareSkcNumber = 213;
                    // 已售完数量
                    responseData.result.alreadySoldOutNumber = 65423;
                    // 售后率
                    responseData.result.qualityAfterSaleRatio90d = 0.037;
                    // 开款价格待确认
                    responseData.result.reviewAdjustPrice = 121;
                    // 今日销量
                    responseData.result.saleVolume = 4468;
                    // 30日销量
                    responseData.result.sevenDaysSaleVolume = 169834;
                    // 90天销量
                    responseData.result.thirtyDaysSaleVolume = 498761;
                    // 信息商品待办
                    responseData.result.waitProductNumber = 156;
                    // 在售商品数量
                    responseData.result.onSaleProductNumber = 367;
                    // 返回修改后的响应对象
                    const modifiedResponse = createModifiedResponse(responseData, response);
                    return modifiedResponse;
                } catch (error) {
                    console.error('处理响应时出错:', error);
                    return response;
                }
            }
            return response;
        }

        if (url.includes(payment)) {
            console.log('Intercepted Fetch API to:', url); // 调试信息

            if (Show) {
                try {
                    const responseBody = await window.readResponseBody(response);
                    const responseData = JSON.parse(responseBody);
                    console.log('返回的JSON数据:', responseData);
                    // 总金额
                    responseData.result.totalAmount = 589736.86;
                    // 可提现金额
                    responseData.result.availableBalance = 519543.02;
                    // 金额字符串
                    responseData.result.totalAmountFormat.digitalText = "589736.86";
                    // 可提现金额字符串
                    responseData.result.availableBalanceFormat.digitalText = "519543.02";
                    // 金额
                    responseData.result.totalBalance = 589736.86;
                    // 返回修改后的响应对象
                    const modifiedResponse = createModifiedResponse(responseData, response);
                    return modifiedResponse;
                } catch (error) {
                    console.error('处理响应时出错:', error);
                    return response;
                }
            }
            return response;
        }

        if (url.includes(follow)) {
            console.log('Intercepted Fetch API to:', url); // 调试信息

            if (Show) {
                try {
                    const responseBody = await window.readResponseBody(response);
                    const responseData = JSON.parse(responseBody);

                    console.log('返回的JSON数据:', responseData);
                    // 关注总人数
                    responseData.result.followerNum = 986542;
                    // 返回修改后的响应对象
                    const modifiedResponse = createModifiedResponse(responseData, response);
                    return modifiedResponse;
                } catch (error) {
                    console.error('处理响应时出错:', error);
                    return response;
                }
            }
            return response;
        }

        if (url.includes(consumer)) {
            console.log('Intercepted Fetch API to:', url); // 调试信息

            if (Show) {
                try {
                    const responseBody = await window.readResponseBody(response);
                    const responseData = JSON.parse(responseBody);

                    console.log('返回的JSON数据:', responseData);
                    responseData.result.consumerPendingProblemCount = 6429;

                    const modifiedResponse = createModifiedResponse(responseData, response);
                    return modifiedResponse;
                } catch (error) {
                    console.error('处理响应时出错:', error);
                    return response;
                }
            }
            return response;
        }

        // 处理原始响应对象
        return response;
    };
})();
